//
//  TracksDataSource.m
//  MantuPlay
//
//  Created by Markus Sintonen on 14.8.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TracksDataSource.h"
#import "TrackDatabase.h"
#import "Track.h"
#import "PlayerViewContoroller.h"
#import "UITableViewExtensions.h"

#define ROW_HEIGHT 46.0
#define FIRSTLABEL_TAG 1
#define SECONDLABEL_TAG 2
#define OBJECTCOUNTLABEL_TAG 3

@implementation TracksDataSource

@synthesize navigationBarTitle, showTrackArtistAndAlbum, sectioned;

- (id)init {
	if ((self = [super init])) {
		orderByTrackNumber = NO;
	
		showTrackArtistAndAlbum = YES;
		
		sectioned = YES;
		
		showTrackCountCell = YES;
		
		self.navigationBarTitle = @"Songs";
	}
	return self;
}

-(id)initWithTracksFromAlbum:(NSString*)album artist:(NSString*)artist genre:(NSString*)genre orderByTrackNumber:(BOOL)orderByTrackNum {
	if ((self = [super init])) {
		if(album) tracksFromAlbum = [[NSString alloc] initWithString:album];
		if(artist) tracksFromArtist = [[NSString alloc] initWithString:artist];
		if(genre) tracksFromGenre = [[NSString alloc] initWithString:genre];
		
		orderByTrackNumber = orderByTrackNum;
		
		showTrackArtistAndAlbum = NO;
		
		sectioned = NO;
		
		showTrackCountCell = NO;
		
		self.navigationBarTitle = @"Songs";
	}
	return self;
}

-(NSArray*)databaseObjectArray {
	return tracks;
}

- (NSString*)name {
	return @"Songs";
}

- (NSString*)navigationBarName {
	return navigationBarTitle;
}

-(CGFloat)rowHeight {
	return ROW_HEIGHT;
}

-(BOOL)showSectioned {
	return sectioned;
}

-(BOOL)extraRowAtTop {
	return YES;
}
-(BOOL)extraRowAtBottom {
	return ([tracks count] > 5);
}

-(void)loadData {
	if(!tracks) {
		NSArray *tempTracks = [[TrackDatabase sharedTrackDatabase] tracksFromAlbum:tracksFromAlbum artist:tracksFromArtist genre:tracksFromGenre orderByTrackNumber:orderByTrackNumber];
		tracks = [[NSArray alloc] initWithArray:tempTracks];
	}
}
-(void)unloadData {
	[tracks release];
	tracks = nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForObject:(id)obj atIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"];
	UILabel *trackLabel;
	UILabel *albumArtistLabel;
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"TrackCell"] autorelease];
		
		trackLabel = [[[UILabel alloc] initWithFrame:CGRectMake(1.0, 1.0, 320.0-1.0, ROW_HEIGHT/2.0-1.0)] autorelease];
		trackLabel.font = [UIFont boldSystemFontOfSize:17.0];
		trackLabel.tag = FIRSTLABEL_TAG;
		[cell.contentView addSubview:trackLabel];
		
		albumArtistLabel = [[[UILabel alloc] initWithFrame:CGRectMake(1.0, ROW_HEIGHT/2.0, 320.0-1.0, ROW_HEIGHT/2.0-1.0)] autorelease];
		albumArtistLabel.font = [UIFont systemFontOfSize:14.0];
		albumArtistLabel.textColor = [UIColor grayColor];
		albumArtistLabel.tag = SECONDLABEL_TAG;
		[cell.contentView addSubview:albumArtistLabel];
	} else {
		trackLabel = (UILabel *)[cell.contentView viewWithTag:FIRSTLABEL_TAG];
		albumArtistLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
	}
	
	Track *track = (Track*)obj;
	if(showTrackArtistAndAlbum) {
		if(trackLabel.superview == nil) [cell.contentView addSubview:trackLabel];
		if(albumArtistLabel.superview == nil) [cell.contentView addSubview:albumArtistLabel];
	
		trackLabel.text = track.title;
		albumArtistLabel.text = [NSString stringWithFormat:@"%@ - %@", track.album, track.artist];
	} else {
		[trackLabel removeFromSuperview];
		[albumArtistLabel removeFromSuperview];
		
		cell.text = track.title;
	}
	
	return cell;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtTopIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShuffleCell"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"ShuffleCell"] autorelease];
	}
	cell.text = @"Shuffle";
	
	return cell;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtBottomIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ObjectCountCell"];
	UILabel *objectCountLabel;
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"ObjectCountCell"] autorelease];
		
		objectCountLabel = [[[UILabel alloc] initWithFrame:CGRectMake(1.0, 1.0, 320.0-1.0, ROW_HEIGHT-2.0)] autorelease];
		objectCountLabel.tag = OBJECTCOUNTLABEL_TAG;
		objectCountLabel.textColor = [UIColor grayColor];
		objectCountLabel.textAlignment = UITextAlignmentCenter;
		[cell.contentView addSubview:objectCountLabel];
	} else {
		objectCountLabel = (UILabel *)[cell.contentView viewWithTag:OBJECTCOUNTLABEL_TAG];
	}
	objectCountLabel.text = [NSString stringWithFormat:@"%d songs", [tracks count]];
	
	return cell;
}

-(id)tableView:(UITableView *)tableView viewControllerForSelectedObject:(id)obj atIndexPath:(NSIndexPath *)indexPath {
	unsigned int indexOfSelectedTrack = [tracks indexOfObjectIdenticalTo:obj];
	
	NSArray *tempPlaylist = [tracks subarrayWithRange:NSMakeRange(indexOfSelectedTrack, [tracks count]-indexOfSelectedTrack)];
	NSArray *playlist = [tempPlaylist arrayByAddingObjectsFromArray:
									  [tracks subarrayWithRange:NSMakeRange(0, indexOfSelectedTrack)]];
	PlayerViewContoroller *player = [[PlayerViewContoroller alloc] initWithTrackPlaylist:playlist];
	return player;
}

- (void)dealloc {
	[tracks release];
	[navigationBarTitle release];
	[tracksFromAlbum release];
	[tracksFromArtist release];
	[tracksFromGenre release];
	[super dealloc];
}

@end
